package com.my.study.hutool.core.io;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.my.study.io.properties.PropertiesDelegator;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author: yidujun
 * @create: 2021/07/02 16:23
 */
@Slf4j(topic = "c.ModifyProperties")
public class ModifyProperties {

    private static final String PATH = "my.properties";

    public static void main(String[] args) throws IOException {
        PropertiesDelegator delegator = new PropertiesDelegator(PATH);

        log.debug("key: {}, value: {}", "password", delegator.get("password"));

        delegator.update("password", "8938798");
    }

    private static void testManual() throws IOException {
        // 加载文件 -> 内存
        Properties properties = new Properties();
        properties.load(FileUtil.getInputStream(PATH));
        System.out.println(properties.getProperty("password"));
        // 修改配置文件
        properties.setProperty("password", "127");
        // 重新写入
        FileOutputStream fos = new FileOutputStream(FileUtil.file(PATH));
        properties.store(fos, "hhh");

        System.out.println(properties.getProperty("password"));

        IoUtil.close(fos);
    }

}
